package crud.client.dialog;

import com.google.gwt.event.shared.HandlerManager;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;

import crud.client.data.authentication.User;
import crud.client.event.AuthenticatedUserEvent;
import crud.client.service.ServiceLocator;
import crud.client.util.GenericCallback;

public class AuthenticationDialog  extends Window 
{
	private Label label = new Label();
	
	public AuthenticationDialog(final HandlerManager handlerManager)
	{
		setTitle("Authentification");
		Canvas canvas = new Canvas();
		canvas.setTop(30);
		this.addChild(canvas);
		canvas.addChild(label);
		label.setWrap(false);
		
		// Dynamic form
		final DynamicForm dynamicForm = new DynamicForm();
		canvas.addChild(dynamicForm);
		
		final TextItem loginTextItem = new TextItem("Login");
		final PasswordItem passwordTextItem = new PasswordItem("Pasword");
		loginTextItem.setRequired(true);
		passwordTextItem.setRequired(true);
		
		dynamicForm.setFields(loginTextItem, passwordTextItem);
		
		IButton validateButton = new IButton("Valider");
		validateButton.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				// Forms validation
				if (dynamicForm.validate() == false)
				{
					return;
				}
				String login = (String) loginTextItem.getValue();
				String password = (String) passwordTextItem.getValue();
				
				GenericCallback<User> userAuthenticationCallback = new GenericCallback<User>()
				{

					@Override
					public void onSuccess(User user)
					{
						if (user != null)
						{
							handlerManager.fireEvent(new AuthenticatedUserEvent(user));
							AuthenticationDialog.this.hide();
						} else
						{
							setError("Authentification échouée");
						}
					}
				};
				ServiceLocator.getServiceBase().authenticate(login, password, userAuthenticationCallback);
			}
		});
		
////		IButton cancelButton = new IButton("Annuler");
////		cancelButton.addClickHandler(new ClickHandler()
////		{
////
////			@Override
////			public void onClick(ClickEvent event)
////			{
////				AuthenticationDialog.this.hide();
////			}
////		});
////		
//		canvas.addChild(cancelButton);
		canvas.addChild(validateButton);
		
		// Positions
		loginTextItem.setTop(30);
		passwordTextItem.setTop(50);
		label.setTop(50);
//		cancelButton.setTop(70);
		validateButton.setTop(70);
	
		loginTextItem.setLeft(10);
		passwordTextItem.setLeft(10);
		label.setLeft(10);
//		cancelButton.setLeft(10);
		validateButton.setLeft(180);
		
		canvas.setWidth(310);
		canvas.setHeight(90);
		this.setWidth(310);
		this.setHeight(140);
		this.setTop(100);
		this.setLeft(300);

		this.draw();
	}

	public void setError(String informationText)
	{
		label.setContents("<div style=\"color: red;\">" + informationText + "</div><br/>");
	}
}
